package com.example.springboot.util;

import com.example.springboot.bean.FileBean;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by tangmayao on 2018/4/9.
 */
public class FileUtil {

    /**
     * 递归遍历将非文件夹类型文件放入到list中
     */
    public static List<FileBean> getFolderFiles(String folder) throws IOException {
        List<FileBean> fileBeanList = new LinkedList<>();
        File folderFile = new File(folder);
        if (folderFile.isDirectory()) {

            File[] listFiles = folderFile.listFiles();
            if (listFiles != null) {
                for (File file : listFiles) {
                    fileBeanList.addAll(getFolderFiles(file.getAbsolutePath()));
                }
            }
        } else {

            FileBean fileBean = new FileBean();
            fileBean.setFileName(folderFile.getName());
            fileBean.setPath(folderFile.getAbsolutePath());
            fileBean.setModified(folderFile.lastModified());
            fileBean.setContent(new String(Files.readAllBytes(Paths.get(folder))));
            fileBeanList.add(fileBean);
        }

        return fileBeanList;
    }
}